<template>
  <div>
    <div style="width: 100%; height: 100vh; overflow: hidden" class="bgImg">
      <div style="width: 500px; height: 300px; background-color:rgba(9, 50, 113 ,0.2); border-radius: 10px;
        margin: 50px auto;">

        <div style="width: 260px; margin: 0 auto; padding-top: 30px">
          <div class="item" style="font-size: 20px; color: white">登录</div>
          <div class="item">
            <input type="text" class="inputItem" placeholder="请输入用户名" v-model="user.username">
          </div>

          <div class="item">
            <input type="password" class="inputItem" placeholder="请输入密码" v-model="user.password">
          </div>
          <div class="item">
            <button class="loginBtn" @click="login">登录</button>
          </div>
        </div>


      </div>
    </div>
  </div>
</template>

<script>
import API from "@/utils/request";
import {resetRouter} from "@/router";
import {setMenu} from "@/utils/manage";

export default {
  name: "Login",
  data() {
    return {
      user: {}
    }
  },
  methods: {
    login() {
      if (!this.user.username) {
        this.$message({
          type: "warning",
          message: "请输入用户名"
        })
        return;
      }
      if (!this.user.password) {
        this.$message({
          type: "warning",
          message: "请输入密码"
        })
        return;
      }

      API.post("/api/user/login", this.user).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "登录成功"
          })
          sessionStorage.setItem("user", JSON.stringify(res.data))
          // 重置路由
          resetRouter(JSON.parse(JSON.stringify(res.data.permission)))
          this.$router.replace("/manage/home")

        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.bgImg {
  background: url("../assets/bg.jpg") no-repeat;
  background-size: 100% 100vh;
}

.item {
  text-align: center;
  padding: 10px 0
}

.loginBtn {
  background: forestgreen;
  border-radius: 5px;
  color: white;
  width: 100%;
  padding: 10px;
  cursor: pointer
}

.inputItem {
  width: 100%;
  padding: 10px;
  border-radius: 5px;
}
</style>
